Kwik.Page.include("Kwik.Data.CommandCollection.js");
Kwik.Page.include("Kwik.Data.ParameterCollection.js");
Kwik.Page.include("Kwik.Data.Parameter.js");

/**
* Command object which can be executed with a connector
* @extends Kwik.Object
* @requires Kwik.Object
* @requires Kwik.Data.ParameterCollection
* @constructor
* @created 12:37 AM 7/29/2009
* @modified 12:38 AM 7/29/2009
*/
Kwik.Data.Command = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Object");
	this.__class.push("Kwik.Data.Command");

	/** Reference to a parent command collection object if any @type Kwik.Data.CommandCollection */
	this.parent = null;
	/** Name of the command @type String */
	this.name = "";
	/** Collection of parameters @type Kwik.Data.ParameterCollection */
	this.parameters = new Kwik.Data.ParameterCollection({parent:this});
	/** Connector to execute this command @type Kwik.Data.Connector */
	this.connector = null;

	/** Execute this command with the supplied connector or the currently defined @type Kwik.Data.Command */
	this.execute = function Command_execute(connector)
	{
		connector = connector || this.connector;
		if(!connector){throw new Error("Kwik.Data.Command.execute: Can't execute command, no valid connector specified.");}
		
		var cancel = this.fireEvent("onbeforeexecute");
		if(cancel === true){return;}

		this.connector = connector;

		connector.detachEvent("onexecute", this.handlers.onexecute);
		connector.attachEvent("onexecute", this.handlers.onexecute);
		connector.detachEvent("onabort", this.handlers.onabort);
		connector.attachEvent("onabort", this.handlers.onabort);
		connector.execute(this);

		return this;
	};

	/** Get the table if this command is in hierarchy @type Kwik.Data.Table */
	this.getTable = function Command_getTable()
	{
		return this.parent != null? this.parent.parent != null? this.parent.parent: null: null;
	};

	/** Configure this parameter object @type Kwik.Data.CommandConfig */
	this.configure = function Comman_configure(config)
	{
		config = this.Kwik$Object.configure(config);
		if(config.parent){this.parent = config.parent;}
		if(config.name){this.name = config.name;}
		if(config.connector){this.connector = config.connector;}
		if(config.parameters)
		{
			for(e in config.parameters)
				if(config.parameters[e] != Object.prototype[e])
					this.parameters.add(e, new Kwik.Data.Parameter({name: e, value: config.parameters[e]}));
		}
		return config;
	};

	/** Event handler for connector's onexecute event */
	this._onexecute = function Command__onexecute()
	{
		connector.detachEvent("onexecute", this.handlers.onexecute);
		connector.detachEvent("onabort", this.handlers.onabort);
		this.fireEvent("onexecute");
	};

	/** Event handler for connector's onabort event */
	this._onabort = function Command__onabort()
	{
		connector.detachEvent("onabort", this.handlers._onabort);
		connector.detachEvent("onexecute", this.handlers.onexecute);
		this.fireEvent("onabort");
	};

	/** @ignore */
	this.main = function(config)
	{
		this.handlers = {};
		this.handlers.onexecute = new Function(this.referenceString() + "._onexecute();");
		this.handlers.onabort = new Function(this.referenceString() + "._onabort();");
		if(config){this.configure(config);}
	}; this.main(config);
};

/**
* Data command configuration object
* @constructor
* @created 12:21 AM 7/29/2009
* @modified 12:21 AM 7/29/2009
*/
Kwik.Data.CommandConfig = function()
{
	/** Reference to a parent command collection object if any @type Kwik.Data.ParameterCollection */
	this.parent = null;
	/** Name of the column @type String */
	this.name = "";
	/** Parameter list @type Object */
	this.parameters = null;
	/** Parameter list @type Kwik.Data.Connector */
	this.connector = null;
};

